package com.mini.common.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 字段自动填充
 */
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时填充创建时间和更新时间
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.strictInsertFill(metaObject, "gmtCreate", () -> now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "gmtModified", () -> now, LocalDateTime.class);
    }

    /**
     * 更新时填充更新时间
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "gmtModified", LocalDateTime::now, LocalDateTime.class);
    }
}
